package com.sne.sys.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sne.sys.dto.SysLogDTO;
import com.sne.sys.entity.SysLog;
import com.sne.sys.entity.SysLoginLog;
import com.sne.sys.entity.SysUser;
import com.sne.sys.service.ISysLoginService;
import com.sne.sys.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.regex.Pattern;

/**
 * 登录日志 serviceImpl
 * @author fanghh
 * @date 2020/11/13
 */
@Slf4j
@Service
public class SysLoginServiceImpl implements ISysLoginService {
    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public IPage findPage(Page page, SysLogDTO log) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        if (StringUtils.isNotBlank(log.getState())) {
            criteria.and("state").is(log.getState());
        }
        if (StringUtils.isNotBlank(log.getTitle())) {
            Pattern pattern = Pattern.compile("^.*"+  log.getTitle() +".*$", Pattern.CASE_INSENSITIVE);
            criteria.and("address").regex(pattern);
        }
        if(log.getStartTime()!= null && log.getEndTime() != null){
            criteria.andOperator(Criteria.where("createTime").gte(log.getStartTime()),
                    Criteria.where("createTime").lte(log.getEndTime()));
        }else if(log.getStartTime()!=null && log.getEndTime() == null){
            criteria.and("createTime").gte(log.getStartTime());
        }else if(log.getStartTime()==null && log.getEndTime() != null){
            criteria.and("createTime").lte(log.getEndTime());
        }
        query.addCriteria(criteria).with(Sort.by(Sort.Order.desc("create_time")));
        long total = mongoTemplate.count(query, SysLoginLog.class);
        Pageable pageable = PageRequest.of((int) page.getCurrent()-1, (int) page.getSize());
        List<SysLoginLog> sysLogs = mongoTemplate.find(query.with(pageable), SysLoginLog.class);
        Page logPage = new Page();
        logPage.setRecords(sysLogs);
        logPage.setTotal(total);
        return logPage;
    }

    @Override
    public boolean removeById(Long id) {
        Query query = new Query().addCriteria(Criteria.where("_id").is(id));
        mongoTemplate.remove(query);
        return true;
    }

    @Override
    public SysLog getById(Long id) {
        Query query = new Query().addCriteria(Criteria.where("_id").is(id));
        return mongoTemplate.findOne(query, SysLog.class);
    }

    @Override
    public boolean removeByIds(List<Long> ids) {
        Query query = new Query().addCriteria(Criteria.where("_id").in(ids));
        mongoTemplate.remove(query, SysLog.class);
        return true;
    }

    @Override
    public boolean remove() {
        Query query = new Query();
        mongoTemplate.remove(query);
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean save(SysLoginLog log) {
        mongoTemplate.save(log);
        return true;
    }
}
